const domainName = getApp().globalData.domainName
const dataType = 'json'
const header = {'content-type':'application/x-www-form-urlencoded'}
const method = 'POST'
const responseType = 'text'
// pages/update_jg_pics/update_jg_pics.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    jg_detail_pics:[],
  },

  update(){
    wx.showModal({
      content: '确定开始修改后，直到再次点击“发布”之前，用户将不能查看贵机构海报信息',
      showCancel: true,
      title: '确定要修改机构海报吗？',
      success: res => {
        if(res.confirm){
          let status = 2
          this.update_publish_jg_pics(status)
        }
      },
    })
  },

  publish(){
    wx.showModal({
      showCancel: true,
      title: '确定要发布吗？',
      success: res => {
        if(res.confirm){
          let status = 1
          this.update_publish_jg_pics(status)
        }
      },
    })
  },

  // 确定海报是否发布
  update_publish_jg_pics(status){
    wx.request({
      url: domainName + '/update_publish_jg_pics',
      data: {
        jg_id:this.data.jg_id,
        status:status,
      },
      dataType: dataType,
      enableCache: true,
      enableHttp2: true,
      enableQuic: true,
      header: header,
      method: method,
      responseType: responseType,
      timeout: 0,
      success: res => {
        if(res.data.update_res){
          if(status==1){
            var title = '发布成功！'
          }else if(status==2){
            var title = '海报已撤回'
          }
          wx.showToast({
            title: title,
            duration: 1500,
            icon: 'success',
          })
        }
        this.load_jg_info(this.data.jg_id)
      },
    })
  },

  finish(){
    if(!wx.getStorageSync('off_vibration')){
      getApp().get_vibration()
    }
    wx.navigateBack()
  },

  pre_view(){
    if(!wx.getStorageSync('off_vibration')){
      getApp().get_vibration()
    }
    wx.navigateTo({
      url: '/pages/jg_page/jg_page?jg_id=' + this.data.jg_id + '&pre_view=1',
    })
  },

  // 删除
  delete(e){
    if(!wx.getStorageSync('off_vibration')){
      getApp().get_vibration()
    }
    let index = e.currentTarget.id
    let row_id = this.data.jg_detail_pics[index].row_id
    let pic_url = this.data.jg_detail_pics[index].pic
    wx.showModal({
      content: '确定要删除该图片吗？',
      showCancel: true,
      title: '提示',
      success: res => {
        if(res.confirm){
          wx.request({
            url: domainName + '/delete_jg_pic_status',
            data: {
              row_id:row_id,
              pic_url:pic_url,
            },
            dataType: dataType,
            enableCache: true,
            enableHttp2: true,
            enableQuic: true,
            header: header,
            method: method,
            responseType: responseType,
            timeout: 0,
            success: res => {
              if(res.data.delete_res){
                wx.showToast({
                  title: '完成删除',
                  duration: 1500,
                  icon: 'success',
                })
                this.load_jg_detail_pics(this.data.jg_id)
              }
            },
          })
        }
      },
    })
  },

  // 添加
  add_pic(){
    if(!wx.getStorageSync('off_vibration')){
      getApp().get_vibration()
    }
    let opt = 'upload'
    let pic_id = ''
    let order_index = this.data.jg_detail_pics.length
    this.upload_pic(opt, pic_id, order_index)
  },


  // 修改
  update_pic(e){
    if(!wx.getStorageSync('off_vibration')){
      getApp().get_vibration()
    }
    let index = e.currentTarget.id
    let pic_id = this.data.jg_detail_pics[index].row_id
    let opt = 'update'
    let order_index = index
    this.upload_pic(opt, pic_id, order_index)
  },

  // 上传图片
  upload_pic(opt, pic_id, order_index){
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      success: res => {
        let pic = res.tempFiles[0].tempFilePath
        wx.showLoading({
          title: '正在上传…',
          mask: true,
        })
        wx.uploadFile({
          filePath: pic,
          name: 'files',
          url: domainName + '/upload_jg_pic',
          formData: {
            jg_id:this.data.jg_id,
            user_id:this.data.user_id,
            pic_type:3, //3-门店宣传
            order_index:order_index,
            pic_id:pic_id,
          },
          header: header,
          timeout: 0,
          success: res => {
            wx.hideLoading()
            if(JSON.parse(res.data).pic_id){
              wx.showToast({
                title: '上传完成！',
                duration: 1500,
                icon: 'success',
              })

              // 更新小程序图片
              if(opt=='update'){
                let jg_detail_pics = this.data.jg_detail_pics
                jg_detail_pics[order_index].pic = pic
                this.setData({
                  jg_detail_pics:jg_detail_pics,
                })
              }else if(opt=='upload'){
                this.load_jg_detail_pics(this.data.jg_id)
              }
            }
          },
        })
      },
    })
  },


  // 获取机构信息
  // 加载机构信息
  load_jg_info(jg_id){
    wx.request({
      url: domainName + '/load_jg_info_for_homepage',
      data: {
        jg_id:jg_id,
      },
      dataType: dataType,
      enableCache: true,
      enableHttp2: true,
      enableQuic: true,
      header: header,
      method: method,
      responseType: responseType,
      timeout: 0,
      success: res => {
        let jg_info = res.data.jg_info
        this.setData({
          jg_info:jg_info,
        })
      },
    })
  },

  load_jg_detail_pics(jg_id){
    wx.showLoading({
      title: '加载中…',
    })
    wx.request({
      url: domainName + '/load_jg_detail_pics',
      data: {
        jg_id:jg_id,
        status:1,
      },
      dataType: dataType,
      enableCache: true,
      enableHttp2: true,
      enableQuic: true,
      header: header,
      method: method,
      responseType: responseType,
      timeout: 0,
      success: res => {
        wx.hideLoading()
        this.setData({
          jg_detail_pics:res.data.jg_detail_pics,
        })
      },
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let jg_id = options.jg_id
    this.load_jg_info(jg_id)
    this.load_jg_detail_pics(jg_id)
    this.setData({
      jg_id:jg_id,
      user_id:wx.getStorageSync('user_id')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})